package com.twu.biblioteca.option;

import com.twu.biblioteca.core.Book;
import com.twu.biblioteca.service.BookService;
import com.twu.biblioteca.core.Option;
import com.twu.biblioteca.util.Cli;

import java.util.List;

public class ListBookOption extends Option {
    private BookService bookService;

    public ListBookOption(BookService bookService, Cli cli) {
        super("List Book", cli);
        this.bookService = bookService;
    }

    @Override
    public void execute() {
        cli.output(displayBooks(bookService.getBooks()));
    }

    private String displayBooks(List<Book> books) {
        StringBuilder result = new StringBuilder();
        for (int bookIndex = 0; bookIndex < books.size(); bookIndex++) {
            result.append(displayBook(bookIndex, books.get(bookIndex)));
        }
        return result.toString();
    }

    private String displayBook(int bookIndex, Book book) {
        StringBuilder result = new StringBuilder();
        result.append(bookIndex);
        result.append(":");
        result.append(book.getName());
        result.append("\n");
        return result.toString();
    }
}
